const app = new getApp();

Page({

  /**
   * 页面的初始数据
   */
  data: {
    prompt: '',
    user_info: {},
    mobile: '',
    new_mobile: '',
    code: ''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let that = this;

    app.sendRequest({
      url: "api.php?s=member/getMemberDetail",
      data: {},
      method: "POST",
      success: function (res) {
        let code = res.code;
        if (code == 0) {
          that.setData({
            user_info: res.data.user_info,
            mobile: res.data.user_info.user_tel,
            new_mobile: res.data.user_info.user_tel,
          })
        }
      }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  },

  /**
   * 输入手机号
   */
  inputMobile: function(event) {
    let that = this;

    let new_mobile = event.detail.value;
    
    that.setData({
      new_mobile: new_mobile
    })
  },

  /**
   * 输入手机动态码
   */
  inputCode: function(event) {
    let that = this;

    let code = event.detail.value;

    that.setData({
      code: code
    })
  },

  /**
   * 获取动态码
   */
  getCode: function() {
    let that = this;

    app.sendRequest({
      url: "api.php?s=member/sendBindCode",
      data: {},
      method: "POST",
      success: function (res) {
        console.log(res)
      }
    });
  },

  /**
   * 保存手机号
   */
  saveMobile: function (evnet) {
    let that = this;

    let mobile = that.data.mobile;
    let new_mobile = that.data.new_mobile;

    if (new_mobile == ''){
      app.showBox(that, '手机号不可为空');
      return false;
    }

    let myreg = /^(((13[0-9]{1})|(15[0-9]{1})|(18[0-9]{1})|(17[0-9]{1}))+\d{8})$/;
    if (new_mobile.length != 11 || !myreg.test(new_mobile)) {
      app.showBox(that, '手机号格式不正确');
      return false;
    }

    if (new_mobile == mobile){
      app.showBox(that, '与原手机号一致，无需修改');
      return false;
    }

    //验证手机号
    app.sendRequest({
      url: "api.php?s=login/mobile",
      data: {
        mobile: new_mobile
      },
      header: {
        'content-type': 'application/json' // 默认值
      },
      success: function (res) {
        let code = res.code;
        let data = res.data;
        if(code == 0){
          if(data == true){
            app.showBox(that, '该手机号已存在');
            return false;
          }else{
            that.save(that, new_mobile);
          }
        }
      }
    });
  },

  /**
   * 保存
   */
  save: function(that, mobile){

    app.sendRequest({
      url: "api.php?s=member/modifyMobile",
      data: {
        mobile: mobile
      },
      header: {
        'content-type': 'application/json' // 默认值
      },
      success: function (res) {
        let code = res.code;
        let data = res.data;
        if (code == 0) {
          if(data > 0){
            wx.navigateBack({
              delta: 1
            })
          }else{
            app.showBox(that, '操作失败');
            return false;
          }
        }
      }
    });
  }
})